/*
 *    Copyright 2014 Sam Y Ge
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.strx;

/**
 * <p>
 * 解析器当前状态
 * </p>
 * 
 * <p>
 * 用于解析器解析到特定标签时，向TagHandle发出事件通告时携带解析器当前状态信息供其处理
 * </p>
 * 
 * @see Parser
 * @see TagHandler#onStart(Status)
 * @see TagHandler#onEnd(Status)
 * @see TagHandler#onCData(Status)
 * 
 * @author <a href="mailto:samuel.y@foxmail.com">Sam Y Ge</a>
 */
public final class Status {
	/** TagStack的深度 */
	private final int depth;

	/** 当前路径 */
	private final String path;

	/** 栈顶Tag */
	private final Tag top;

	/**
	 * 构建当前解析器状态
	 * 
	 * @param depth
	 *            TagStack的深度
	 * @param path
	 *            当前路径
	 * @param top
	 *            栈顶Tag
	 */
	Status(TagStack stack, Tag top) {
		this.depth = stack.size();
		this.path = stack.path();
		this.top = top;
	}

	/**
	 * 获取当前TagStack深度
	 * 
	 * @return 当前TagStack深度
	 */
	public int getDepth() {
		return depth;
	}

	/**
	 * 获取当前解析路径
	 * 
	 * @return 当前解析路径
	 */
	public String getPath() {
		return path;
	}

	/**
	 * 获取TagStack栈顶Tag类型
	 * 
	 * @return 栈顶Tag类型
	 */
	public TagType getType() {
		return top.getType();
	}

	/**
	 * 获取TagStack栈顶Tag的属性值
	 * 
	 * @param name
	 *            属性名
	 * @return 对应的属性值，若该属性不存在，则返回null
	 */
	public String getAttribute(String name) {
		return top.getAttributes().get(name);
	}

	/**
	 * 判定TagStack栈顶Tag的属性值是否存在
	 * 
	 * @param name
	 *            属性名
	 * @return 对应的属性是否存在
	 */
	public boolean hasAttribute(String name) {
		return top.getAttributes().containsKey(name);
	}

	/**
	 * 获取TagStack栈顶Tag的文本
	 * 
	 * @return 栈顶Tag的文本，若文本不存在，则返回null
	 */
	public String getText() {
		return top.getText();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		StringBuilder buff = new StringBuilder();
		buff.append("Status [depth=");
		buff.append(depth);
		buff.append(", path=");
		buff.append(path);
		buff.append(", top=");
		buff.append(top);
		buff.append("]");
		return buff.toString();
	}

}
